let prefix = ctx + "manhua/look/classify";
let num = classify_status.num;
let page_Size = classify_status.page;
let url = location.href;
let classify = url.substring(url.lastIndexOf("/") + 1);
let click_classify = '';
let Service_data = [];


$(function () {
    //创建MeScroll对象
    var mescroll = new MeScroll("mescroll", {
        /*  down: {
              use:false,
              auto: false, //是否在初始化完毕之后自动执行下拉回调callback; 默认true
              callback: downCallback //下拉刷新的回调
          },*/
        up: {
            auto: true, //是否在初始化时以上拉加载的方式自动加载第一页数据; 默认false
            isBounce: false, //此处禁止ios回弹,解析(务必认真阅读,特别是最后一点): http://www.mescroll.com/qa.html#q10
            callback: upCallback, //上拉回调,此处可简写; 相当于 callback: function (page) { upCallback(page); }
            toTop: { //配置回到顶部按钮
                src: "/img/huojian.gif", //默认滚动到1000px显示,可配置offset修改
                width: "50",
                height: "60",
                duration: "500"
                //offset : 1000
            }
        }
    });

    function upCallback() {
        //联网加载数据
        getListDataFromNet(function (curPageData) {


                if (classify != '') {
                    let arr = ['quanbu', 'rexue', 'lianai', 'gufeng', 'siaoyuan', 'qihuan', 'chunai', 'weimei', 'riman', 'hanman', 'danvzhu', 'chuanyue', 'mengxi', 'kongbu', 'xuanyi', 'xuanhuan', 'gaoxao', 'dushi', 'zhengnengliang', 'zongcai'];
                    var boot = true;
                    for (let i = classify_status.zero, len = arr.length; i < len; i++) {
                        if (classify === arr[i]) {
                            boot = false;
                        }
                    }
                    if (boot) {
                        $.modal.alertError('参数不合法。。');
                        return;
                    }
                } else {
                    return;
                }

                if ($("#null-Warning").text() === '别再滑了，我也是有底线的^_^..' || $("#null-Warning").text() === '抱歉资源不足，试试其他分类吧^_^..') {
                    //$(".mescroll-upwarp").css("visibility","hidden");
                    //隐藏上拉刷新
                    mescroll.hideUpScroll(true);
                    return;
                }
                if (num === classify_status.num) {
                    $(".all").removeClass('only');
                    $("." + classify + "").addClass('only');
                }

                add({num: num, page: page_Size, type: classify});


                /*if(click_classify != ''){
                    add({num:num,page:pageSize,type:click_classify});
                }else{
                    only(classify);
                }*/


                mescroll.endSuccess(curPageData.length);

            },
            function () {
                //联网失败的回调,隐藏下拉刷新和上拉加载的状态;
                mescroll.endErr();
            });
    }


    /*联网加载列表数据
     在您的实际项目中,请参考官方写法: http://www.mescroll.com/api.html#tagUpCallback
     请忽略getListDataFromNet的逻辑,这里仅仅是在本地模拟分页数据,本地演示用
     实际项目以您服务器接口返回的数据为准,无需本地处理分页.
     * */
    function getListDataFromNet(successCallback, errorCallback) {
        try {
            //联网成功的回调
            successCallback && successCallback(null);
        } catch (e) {
            //联网失败的回调
            errorCallback && errorCallback();
        }
    }

});


let only = (classStr) => {

    let url = location.href.substring(0, location.href.lastIndexOf("/"));

    location.href = url + "/" + classStr;
    //window.history.pushState(null, null, url+"/"+classStr);

    //add({num:num,page:pagesize,type:classStr});

    /*num = 1;
      pagesize = 10;
      $("#articl-1").empty();
      click_classify = classStr;*/
}

let add = (data) => {

    $.operate.request(prefix + "/classifyData", data, function (row) {
        /*关闭遮罩层*/
        $.modal.closeLoading();
        if (row.code == web_status.SUCCESS) {

            let list = row.data.lists;

            let avatar = row.data.avatar;

            if (avatar != null && avatar != '' && avatar != undefined){
                if (avatar != 'no'){
                    $(".right-img div").css("display","none");

                    $(".right-img img")
                        .css("display","block")
                        .attr("src",avatar);
                }
            }
            let width = $(document.body).width();
            if (list.length != web_status.SUCCESS) {
                let html = '';
                for (let i = classify_status.zero, len = list.length; i < len; i++) {
                    Service_data.push(list[i]);
                    let newZhang = list[i].newZhang;

                    if (width === classify_status.body_width) {
                        if (newZhang.length > classify_status.eijht) {
                            newZhang = newZhang.substring(classify_status.zero, classify_status.eijht) + "..";
                        }
                    } else {
                        if (newZhang.length > classify_status.web_dataNumber_ini) {
                            newZhang = newZhang.substring(classify_status.zero, classify_status.web_dataNumber_ini) + "..";
                        }
                    }

                    let name = list[i].name;

                    if (width === classify_status.body_width) {
                        if (name.length > classify_status.eijht) {
                            name = name.substring(classify_status.zero, classify_status.eijht) + "..";
                        }
                    } else {
                        if (name.length > classify_status.web_dataNumber_ini) {
                            name = name.substring(classify_status.zero, classify_status.web_dataNumber_ini) + "..";
                        }
                    }

                    html += ` <div class="articl-1-2-box">
                                    <a href="/manhua/look/over/${list[i].id}" target="_blank">
                                        <img src="${$.haHa.decode(list[i].theCover)}" alt="${list[i].name}" onerror="$.common.errorimg(this)">
                                        <div >
                                            <span class="articl-1-2-box-left">${newZhang}</span>
                                        </div>
                                        <span>${name}</span>
                                    </a>
                                </div>`;

                }
                $(".articl-1-2").append(html);
            } else {
                $.modal.alertError('没有数据...');
            }
        } else if (row.code == web_status.FAIL) {
            $("#null-Warning").remove();
            let msg = row.msg.split(":")[1];
            if (msg.indexOf("|") != -1){
                let arr = msg.split("|");
                $(".right-img div").css("display","none");

                $(".right-img img")
                    .css("display","block")
                    .attr("src",arr[1]);

                $("#articl-1").append(`<div id="null-Warning">${arr[0]}</div>`);

            }else{
                $("#articl-1").append(`<div id="null-Warning">${msg}</div>`);
            }
        } else {
            if (row.code === 3) {
                $.modal.alertError(row.msg);
            }
        }
    });
    num++;
}


/*监听浏览器窗口宽度 变化*/
/* $(window).resize(function () {
      let width = $(document.body).width();
      if(width === classify_status.body_width){
          if(Service_data.length != classify_status.zero){
              let html = '<div class="articl-1-2">';
              let number = classify_status.web_dataNumber_ini;

              for (let i = classify_status.zero,len = Service_data.length ; i < len ; i++){
                  let newZhang = Service_data[i].newZhang;
                  if(newZhang.length > classify_status.eijht){
                      newZhang = newZhang.substring(classify_status.zero,classify_status.eijht)+"..";
                  }
                  let name = Service_data[i].name;
                  if(name.length > classify_status.eijht){
                      name = name.substring(classify_status.zero,classify_status.eijht)+"..";
                  }
                  html += ` <div class="articl-1-2-box">
                                 <a href="#">
                                     <img src="${Service_data[i].theCover}" alt="${Service_data[i].name}" onerror="$.common.errorimg(this)">
                                     <div >
                                         <span class="articl-1-2-box-left" >${newZhang}</span>
                                     </div>
                                     <span>${name}</span>
                                 </a>
                             </div>`;
                  if(i === number){
                      html += `</div><div class="articl-1-2">`;

                      number = number + classify_status.min_dataNumber;

                  }

                  if(i === len - classify_status.num){
                      html += `</div>`;
                  }

              }
              $("#articl-1").empty();
              $("#articl-1").append(html);
          }
      }else {
          if(Service_data.length != classify_status.zero){
              let html = '<div class="articl-1-2">';
              let number = classify_status.web_dataNumber_ini;
              for (let i = classify_status.zero,len = Service_data.length ; i < len ; i++){
                  let newZhang = Service_data[i].newZhang;
                  if(newZhang.length > classify_status.eijht){
                      newZhang = newZhang.substring(classify_status.zero,classify_status.eijht)+"..";
                  }
                  let name = Service_data[i].name;
                  if(name.length > classify_status.eijht){
                      name = name.substring(classify_status.zero,classify_status.eijht)+"..";
                  }
                  html += ` <div class="articl-1-2-box">
                                 <a href="#">
                                     <img src="${Service_data[i].theCover}" alt="${Service_data[i].name}" onerror="$.common.errorimg(this)">
                                     <div >
                                         <span class="articl-1-2-box-left" >${newZhang}</span>
                                     </div>
                                     <span>${name}</span>
                                 </a>
                             </div>`;
                  if(i === number){
                      html += `</div><div class="articl-1-2">`;
                      number = number + classify_status.web_dataNumber;
                  }
                  if(i === len - classify_status.num){
                      html += `</div>`;
                  }

              }
              $("#articl-1").empty();
              $("#articl-1").append(html);
          }
      }
 });*/
